package com.navinfo.platform.trip.common.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Map单例,用于算法读取配置.
 * @author 水哥
 */
public class ConfigMap {
    private static Logger logger = LoggerFactory.getLogger(ConfigMap.class);

    private static final Map<String, Object> conf = new ConcurrentHashMap<>();

    /**
     * 将map放入ConfigMap的conf属性中
     * @param map 配置Map
     */
    public static void init(Map<String, Object> map) {
        //处理Map的key或value中含有null的情况
        Map<String, Object> target = new HashMap<>();
        map.forEach((s, o) -> {
            s = null == s ? "" : s;
            o = null == o ? "" : o;
            target.put(s, o);
        });
        conf.putAll(target);
    }

    public static Map<String, Object> getConf() {
        return conf;
    }

    public static Double getDouble(String key) {
        return Double.parseDouble(getString(key));
    }

    public static Integer getInteger(String key) {
        return Integer.parseInt(String.valueOf(getString(key)));
    }

    public static Long getLong(String key) {
        return Long.parseLong(getString(key));
    }

    public static String getString(String key) {
        Object value=conf.get(key);
        if(value==null||"".equals(value)){
            logger.debug("ConfigMap.getString:{} is null",key);
            value="";
        }

        return String.valueOf(value);
    }
}
